package top.ztf.erp.utils;

import com.alibaba.fastjson.JSONObject;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * 通用的响应类
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ResponseClient {
    //成功
    private static final String SUCCESS = "200";
    //失败
    private static final String FAIL = "500";

    private static final String NO_MESSAGE = "操作成功";

    private static final String ERR_MESSAGE = "操作失败";

    //响应的状态
    private String code;
    //响应消息
    private String message;
    //响应数据
    private Object data;

    /**
     * 请求成功,有数据
     * @param data
     * @return
     */
    public static void success(Object data,HttpServletResponse response) throws IOException {
        response.getWriter().print(JSONObject.toJSONString(new ResponseClient(SUCCESS,NO_MESSAGE,data)));
    }

    /**
     * 请求成功,无数据
     * @return
     */
    public static void success(HttpServletResponse response) throws IOException {
        response.getWriter().print(JSONObject.toJSONString(new ResponseClient(SUCCESS,NO_MESSAGE, null)));
    }

    /**
     * 请求失败,无数据,自定义消息
     * @param msg
     * @return
     */
    public static void fail(String msg,HttpServletResponse response) throws IOException {
        response.getWriter().print(JSONObject.toJSONString(new ResponseClient(FAIL,msg, null)));
    }

    /**
     * 请求失败,无数据,默认消息
     * @return
     */
    public static void fail(HttpServletResponse response) throws IOException {
        response.getWriter().print(JSONObject.toJSONString(new ResponseClient(FAIL,ERR_MESSAGE, null)));
    }

    /**
     *通用得响应体方法
     * @param count service 层返回 0 是失败 ,1是操作成功
     * @param response 响应对象
     * @throws IOException
     */
    public static void common(int count, HttpServletResponse response) throws IOException {
        if (count==0){
            //操作失败
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(FAIL,ERR_MESSAGE, null)));
        }else {
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(SUCCESS,NO_MESSAGE, null)));
        }
    }

    public static void common(boolean count, HttpServletResponse response) throws IOException {
        // true 表示成功  false 表示失败
        if (!count){
            //操作失败
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(FAIL,ERR_MESSAGE, null)));
        }else {
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(SUCCESS,NO_MESSAGE, null)));
        }
    }

    /**
     *
     * @param count service 层返回 0 是失败 ,1是操作成功
     * @param data 我们要返回得数据
     * @param response 响应对象
     * @throws IOException
     */
    public static void common(int count,Object data,HttpServletResponse response) throws IOException {
        if (count==0){
            //操作失败
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(FAIL,ERR_MESSAGE, null)));
        }else {
            response.getWriter().print(JSONObject.toJSONString(new ResponseClient(SUCCESS,NO_MESSAGE, data)));
        }
    }
}
